{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loop\n",
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Loop](https://lh3.googleusercontent.com/Gsy3gvhT5OUjqobdPrCDaNL7B7G8I57KfjTD-cvwZNKjhm0dcHQ1VTZESRnyR3SrbOHhJUrX8UBZ_EO1uJxq3Ddlnxkn6W7F6Nxe7xjqgcqOLfuSN50PSm2vWVIiXxc9z-f_950-cI-NSp5Jd7j592c1yJ9qJySifchPwTPmdzbW2pcQfq5ZE-y3b30pmK1sZGd1U_hh9CjiiFowARUopdY7cz9xkhFg5TGusIxYChZHRmiFqf8eOgZpEDLH-aPNTygyQnArbE61YmUPjzj5-XA2yiXfqfbtS7lzPkMnQOuE5HImeaXnOzEKuJnGGBLi66k22PslajI-_RjyCh_YKsohjThxT-ocK6cw4v6mdDb3qq4etGdRePkhB3a1rlI-7NT62yWphsmbi4OHP2WSOJjpGfblOB-Ezi5irhofUIQJbUHSBDiMQ-fCzZGeJt_6xCXlg-AulJD51CuTHDd4F-sf9QCigiTfFKy0bYriXIhwq_l1MwhUGqWZ5UAamrf1H_HsVVK3UTUfwJN41s40gaH8vMI9fFr2cDlcivRFk43WaK9Ik9Nri1vYr7OeB8_tIMjp6Br2AP_2xbkCXRlyrR_5vsjgkAjuc3dgbiLYLJ37rneo2FTs1R5V48mi85oQjTN3x_Gc_mjExIGoq3tjmMI1hHLRaBWehEb-1QlGsMYo9qa1hQr9LcHcqg=w450-h300-no)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ảnh từ [PenJee](https://blog.penjee.com/top-5-animated-gifs-explain-loops-python/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### I. While Loop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 1. While với hàm print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "while a < 10:\n",
    "    print(a)\n",
    "    a += 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Biến a sẽ tăng lên 2 từ 1 cho đến 9 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 2. While với hàm if"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 is one of 0 factors\n",
      "5 is one of 5 factors\n",
      "5 is one of 10 factors\n",
      "5 is one of 15 factors\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "while a < 20:\n",
    "    if a % 5 == 0:\n",
    "        print (\"5 is one of %d factors\" %a)\n",
    "    a += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 3. While với break\n",
    "Thoát khỏi vòng lặp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Hoàng\", \"Hòa\", \"Dương\", \"Hùng\", \"Ngọc\", \"Lâm\", \"Đạt\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "names_length = len(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Khá vỡ vòng lặp khi đến phần tử Ngọc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hoàng\n",
      "Hòa\n",
      "Dương\n",
      "Hùng\n",
      "Ngọc\n"
     ]
    }
   ],
   "source": [
    "a = 0\n",
    "while a < names_length:\n",
    "    print names[a]\n",
    "    if names[a] == \"Ngọc\":\n",
    "        break\n",
    "    a += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 4. While else"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hàm else sẽ được thực hiện khi a không còn nhỏ hơn 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n",
      "End while\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "while a < 10:\n",
    "    print(a)\n",
    "    a += 2\n",
    "else:\n",
    "    print \"End while\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### II. For In"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 1. For với hàm print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "office_stuffs = [\"Ballpoint Pen\", \"Pencil\", \"Glue Stick\", \"Erasor\", \"Pencil sharpener\", \"Stapler\", \"Scissors\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ballpoint Pen\n",
      "Pencil\n",
      "Glue Stick\n",
      "Erasor\n",
      "Pencil sharpener\n",
      "Stapler\n",
      "Scissors\n"
     ]
    }
   ],
   "source": [
    "for item in office_stuffs:\n",
    "    print item"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 2. For với hàm else\n",
    "Hàm else sẽ được thực hiện khi hàm lặp kết thúc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "other_stuffs = [\"Paper Clip\", \"Binder Clip\", \"Pushpin\", \"Sticky labels\", \"Ring Binder\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Paper Clip\n",
      "Binder Clip\n",
      "Pushpin\n",
      "Sticky labels\n",
      "Ring Binder\n",
      "End for loop\n"
     ]
    }
   ],
   "source": [
    "for item in other_stuffs:\n",
    "    print item\n",
    "else:\n",
    "    print \"End for loop\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### III. Đào sâu "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Đọc thêm [Functions creating iterators for efficient looping](https://docs.python.org/2/library/itertools.html)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
